package ServiceDataFresh

import (
	"strconv"
	"strings"
	"ustcLab/server/dao/bean"
)

func DataTransfer(request *bean.TPythonDataInfo) (error, *bean.DashBoardResponse) {
	dashboard := &bean.DashBoardResponse{} //声明指针和不是指针啥区别?
	dashboard.NServer = "MINDRAY"
	dashboard.Data = make([]*bean.TDashBoard, 6)
	//血氧
	DataPoints, ok := request.Data[0]["PULS_OXIM_SAT_O2"].(string)
	if ok == false {
		DataPoints = "0"
	}
	DataPoints = "{0:" + DataPoints + "}"
	dashboard.Data[0] = &bean.TDashBoard{
		FName:            "FSao2",
		FState:           "in",
		FDataType:        "SegmentedDataType",
		FStartTime:       0,
		FIntervalTime:    0,
		FContinuousTime:  0,
		FTotalDataNumber: 1,
		FDataPoints:      DataPoints,
	}
	//心率
	DataPoints, ok = request.Data[1]["PULS_OXIM_PULS_RATE"].(string)
	if ok == false {
		DataPoints = "0"
	}
	DataPoints = "{0:" + DataPoints + "}"
	dashboard.Data[1] = &bean.TDashBoard{
		FName:            "FHr",
		FState:           "in",
		FDataType:        "SegmentedDataType",
		FStartTime:       0,
		FIntervalTime:    0,
		FContinuousTime:  0,
		FTotalDataNumber: 1,
		FDataPoints:      DataPoints,
	}
	//血压指标峰值BLD_PERF_INDEX
	DataPoints, ok = request.Data[2]["BLD_PERF_INDEX"].(string)
	if ok == false {
		DataPoints = "0"
	}
	DataPoints = "{0:" + DataPoints + "}"
	dashboard.Data[2] = &bean.TDashBoard{
		FName:            "FBpPerf",
		FState:           "in",
		FDataType:        "SegmentedDataType",
		FStartTime:       0,
		FIntervalTime:    0,
		FContinuousTime:  0,
		FTotalDataNumber: 1,
		FDataPoints:      DataPoints,
	}
	// ECG_ELEC_POTL_II 导联电压（波形数据）
	length := 499 //数据上写500个数据点，实际只有499个数据点
	//fmt.Println(length)
	strPoints, ok := (request.Data[4]["waveData"].([]interface{}))[0].(map[string]interface{})["ECG_ELEC_POTL_II"].(string)
	if ok == false {
		strPoints = ""
		length = 0
	}
	DataPoints = strConbine(strPoints, length)
	dashboard.Data[3] = &bean.TDashBoard{
		FName:            "FElecPotl",
		FState:           "in",
		FDataType:        "SegmentedDataType",
		FStartTime:       0,
		FIntervalTime:    0,
		FContinuousTime:  0,
		FTotalDataNumber: length,
		FDataPoints:      DataPoints,
	}
	//fmt.Println(dashboard.Data[3].FName)
	//fmt.Println(dashboard.Data[3].FDataPoints)

	// "PULS_OXIM_PLETH" 指尖容积（波形数据）
	strlen, ok := (request.Data[4]["waveData"].([]interface{}))[1].(map[string]interface{})["ATTR_SAMP_RATE"].(string)
	if ok == false {
		length = 0
	} else {
		length, _ = strconv.Atoi(strlen)
	}
	strPoints, ok = (request.Data[4]["waveData"].([]interface{}))[1].(map[string]interface{})["PULS_OXIM_PLETH"].(string)
	if ok == false {
		strPoints = ""
	}
	DataPoints = strConbine(strPoints, length)
	dashboard.Data[4] = &bean.TDashBoard{
		FName:            "FPleth",
		FState:           "in",
		FDataType:        "SegmentedDataType",
		FStartTime:       0,
		FIntervalTime:    0,
		FContinuousTime:  0,
		FTotalDataNumber: length,
		FDataPoints:      DataPoints,
	}
	//fmt.Println(dashboard.Data[4].FName)
	//fmt.Println(dashboard.Data[4].FDataPoints)
	// IMPED_TTHOR 未知指标（波形数据)
	strlen, ok = (request.Data[4]["waveData"].([]interface{}))[2].(map[string]interface{})["ATTR_SAMP_RATE"].(string)
	if ok == false {
		length = 0
	} else {
		length, _ = strconv.Atoi(strlen)
	}
	strPoints, ok = (request.Data[4]["waveData"].([]interface{}))[2].(map[string]interface{})["IMPED_TTHOR"].(string)
	if ok == false {
		strPoints = ""
	}
	DataPoints = strConbine(strPoints, length)
	dashboard.Data[5] = &bean.TDashBoard{
		FName:            "FImpedTthor",
		FState:           "in",
		FDataType:        "SegmentedDataType",
		FStartTime:       0,
		FIntervalTime:    0,
		FContinuousTime:  0,
		FTotalDataNumber: length,
		FDataPoints:      DataPoints,
	}
	//fmt.Println(dashboard.Data[5].FName)
	//fmt.Println(dashboard.Data[5].FDataPoints)
	//for _, v := range dashboard.Data {
	//	_, err := daoimpl.InsertDashBoard(v)
	//	if err != nil {
	//		log.Fatalln("[ServiceDataFreshDaemon] err:", err)
	//		continue
	//	}
	//}

	return nil, dashboard
}

//波形图点字符串格式转换
func strConbine(strPoints string, length int) string {

	var build strings.Builder
	build.WriteString("{")
	var Points []string = strings.Split(strPoints, "^")
	for i := 0; i < length-1; i++ {
		str := strconv.Itoa(i) + ":" + Points[i] + ","
		build.WriteString(str)
	}
	str := strconv.Itoa(length-1) + ":" + Points[length-1] + "}"
	build.WriteString(str)
	return build.String()
}
